import icon_marker from '../../../assets/mark_bs.png';
import { get } from 'lodash-es';

export class AMapPoiSearch {
  constructor(map) {
    this.map = map;
  }
  // 初始化poi插件
  init(inputId, autocompleteOption, placeSearchOption) {
    return new Promise((resolve) => {
      AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], () => {
        // 实例化Autocomplete
        const autoOption = Object.assign({}, autocompleteOption, {
          // input 为绑定输入提示功能的input的DOM ID
          input: inputId,
        });
        /* 不给 AMap.PlaceSearch 提供 map 实例, AMap.PlaceSearch 就不会自动渲染 marker */
        const searchOption = Object.assign({}, placeSearchOption);
        this.autoComplete = new AMap.Autocomplete(autoOption);
        this.placeSearch = new AMap.PlaceSearch(searchOption);
        this.bindEvent();
        resolve();
      });
    });
  }
  bindEvent() {
    AMap.event.addListener(this.placeSearch, 'complete', (e) => {
      this.onSearch(Object.assign({ ok: e.info === 'OK' }, e.poiList));
    });
    AMap.event.addListener(this.autoComplete, 'select', (e) => {
      if (e.poi.location) {
        this.onSearch({ ok: true, count: 1, pois: [e.poi] });
      } else {
        this.placeSearch.search(e.poi.name);
      }
    });
  }
  unbindEvent() {
    AMap.event.removeListener(this.autoComplete, 'select');
    AMap.event.removeListener(this.placeSearch, 'complete');
  }
  match(keyword) {
    return new Promise((resolve) => {
      this.autoComplete.search(keyword, (status, result) => {
        resolve(result);
      });
    });
  }
  search(keyword) {
    this.placeSearch.search(keyword);
  }
  clear() {
    if (this.placeSearch) {
      this.placeSearch.clear();
    }
    if (this.overlays) {
      this.overlays.clearMarkers();
    }
  }
  getMarkerOption(data) {
    return {
      content: `<img src="${icon_marker}" style="width: 18px;height: 30px"/>`,
      offset: new AMap.Pixel(-9, -30),
      title: get(data, 'name'),
      label: { content: get(data, 'name'), direction: 'top', offset: new AMap.Pixel(0, 0) },
    };
  }
  onSearch(res) {
    /*这里是对外输出结果的接口*/
    // console.log(res);
    if (!this.overlays) {
      this.overlays = new AMap.MarkerClusterer();
      this.overlays.setMap(this.map);
    }
    this.overlays.clearMarkers();

    if (res && res.pois && res.pois.length) {
      const markers = res.pois.reduce((res, item) => {
        if (!item || !item.location) return res;
        const option = this.getMarkerOption(item);
        const marker = new AMap.Marker(
          Object.assign({}, option, {
            extData: item,
            position: item.location,
          })
        );
        return res.concat([marker]);
      }, []);
      this.overlays.addMarkers(markers);
      this.overlays.on('click', (e) => {
        this.map.setFitView(e.target);
        if (typeof this.onMarkerClick === 'function') {
          this.onMarkerClick(e.target.getExtData(), e.target);
        }
      });
      this.map.setFitView(markers);
    }
  }
  onMarkerClick(e) {}
}
